# Apply to: exit_1_cands.csv
# Description: W-NOMINATE of San Francisco Mayoral Candidates

# Remove all objects just to be safe

rm(list=ls(all=TRUE))


# Load appropriate packages

# Standard

library(MASS)
library(stats)


# W-NOMINATE

library(pscl)
library(wnominate)
library(gdata)
library(ellipse)

# Read in data

sfmay1 <- read.table(".../1805 Roadmaps/Figures A7 and A8/exit_1_cands.csv", header=TRUE, sep=",")

dim(sfmay1)

head(sfmay1[,1:15])


## Format data

sfmay <- as.vector(sfmay1)
sfmay.2 <- sfmay[,-c(1,2,3)]
sfmay.names <- sfmay[,2]
legData <- matrix(sfmay[,3], length(sfmay[,3]),1)


## Create rollcall object

rc <- rollcall(data=sfmay.2, yea=c(1), nay=c(6), missing=c(9), notInLegis=0,
               legis.names=sfmay.names,
               legis.data=legData,
               desc="SF Mayoral Candidates 2011", source="sfpublicpress.org")


# Run WNOMINATE
# Supervisor Michela Alioto-Pier used to define polarity

set.seed(87332)

result <- wnominate(rcObject=rc, polarity=c(2,2))

summary(result)


# Assume 1-D model

set.seed(547843)

result1 <- wnominate(rcObject=rc, polarity=c(2), dim=1)

scores <- result1$legislators
write.csv(scores, ".../1805 Roadmaps/Figures A7 and A8/sfmay_1_scores.csv")


# Clean up

# rm(list=ls(all=TRUE))

# End
